<?php

namespace App\Models;

use App\Models\traits\serializeDate;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Village extends Model
{
    use HasFactory, serializeDate;

    protected $guarded = [];

    protected $hidden = ['pivot'];
    //附加当前村的干部信息
    public $appends = ['admins'];

    public function getAdminsAttribute()
    {
        return $this->wxusers()->select(['wxuser_id', 'avatarUrl', 'nickName'])->get();
    }

    //中间表多对多，村与管理员关联
    public function wxusers()
    {
        return $this->belongsToMany(Wxuser::class, 'admin_village')->withTimestamps();
    }

    //与 政策咨询 关联 1对多
    public function consultations()
    {
        return $this->hasMany(Consultation::class);
    }

    //与 反应诉求 关联 1对多
    public function appeals()
    {
        return $this->hasMany(Appeal::class);
    }

    //与 举报干部 关联 1对多
    public function reports()
    {
        return $this->hasMany(Report::class);
    }
}
